package com.lovo.frame;

import com.lovo.service.UserService;
import com.lovo.service.impl.UserServiceImpl;
import com.lovo.util.Util;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ChangeSecret extends JPanel implements ActionListener {
    private ATM frame;
//    private Icon bgOff = Util.getIcon("/images/buttonOff.jpg");
//    private Icon bgsure = Util.getIcon("/images/buttonSure.jpg");
    private JButton offButton = new JButton("退出");
    private JButton sureButton = new JButton("确认");
    private JPasswordField textField = new JPasswordField();
    private JPasswordField textFieldNew = new JPasswordField();
private UserService userService=new UserServiceImpl();
    ChangeSecret(ATM frame) {
        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);
        sureButton.setBounds(300, 500, 100, 50);
        this.add(sureButton);
        sureButton.addActionListener(this);
        offButton.setBounds(300, 600, 100, 50);
        this.add(offButton);
        offButton.addActionListener(this);
        JLabel label = new JLabel("输入原密码");
        label.setBounds(350, 300, 150, 20);
        this.add(label);
        textField.setBounds(500, 300, 100, 20);
        this.add(textField);
        JLabel labelNew = new JLabel("输入新密码");
        labelNew.setBounds(350, 400, 150, 20);
        this.add(labelNew);
        textFieldNew.setBounds(500, 400, 100, 20);
        this.add(textFieldNew);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == offButton) {
            frame.switchPanel("menu");
        } else if (e.getSource() == sureButton) {
//            if (frame.getList().get(frame.getLoginPanel().getIndex()).getPwd().equals(textField.getText())&&textFieldNew.getText().matches("^[0-9]{6}$")) {
//                frame.getList().get(frame.getLoginPanel().getIndex()).setPwd(textFieldNew.getText());
//                JOptionPane.showMessageDialog(this, "修改成功,请重新登录");
//                frame.switchPanel("login");
//            }else {
//                JOptionPane.showMessageDialog(this,"修改失败");
//            }
            String oldSecret=String.valueOf(textField);
            String newSecret=String.valueOf(textFieldNew);
            try {
                userService.changeSecret(frame.getLoginUser().getId(),oldSecret,newSecret);
                JOptionPane.showMessageDialog(this, "修改成功,请重新登录");
                frame.switchPanel("login");
            } catch (Exception exception) {
                JOptionPane.showMessageDialog(this,"修改失败");
            }
        }
    }
}
